<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/transaction/allocation_index.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/project_device_alert.css">
</script>
<style scoped>
    .deleteButton{
        height: 20px;
        line-height: 20px;
        padding: 0 5px;
        font-size: 10px;
        color:#fff;
        background:#FF4C4C;
        border:0px;
        display: flex;
        vertical-align: middle;
    }
</style>
<div class="layui-fluid">
    <div class="layui-table-title">
        工单详细信息
    </div>
    <div class="layui-table-box-1">
        <ul class="layui-row">
            <li class="layui-col-md4 layui-col-sm4">
                <div>工单名称：<span id='tcReqName'></span></div>
                <div>OA申请编号：<span id='fil02'></span></div>
                <div>申请日期：<span id="fil10"></span></div>
            </li>
            <li class="layui-col-md4 layui-col-sm4">
                <div>工单编号：<span id='fil01'></span></div>
                <div>报废时间：
                    <input type="text" name="time" id="test1" autocomplete="off">
                </div>
                <div>
                    设备所属公司：<span id="oldazp"></span>
                </div>
            </li>
            <li class="layui-col-md4 layui-col-sm4">
                <div>工单状态：<span id="fil05"></span></div>
                <div>申请人：<span id="newazp"></span></div>

            </li>
        </ul>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">报废说明:</label>
            <div class="layui-input-block">
                <textarea name="desc" id="fil21" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="desc" id="fil07" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="footer layui-btn-containner">

            <button class="hold layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-bc">保存</button>
        </div>


    </div>
    <div class="title-btn layui-btn-containner">
        <button class="add layui-btn layui-btn-radius layui-btn-primary" style="background: #4C96CF;">
            <i class="add-icon layui-icon layui-icon-add-circle" style="font-size: 15px;"></i>
            <span class="btn-inner">添加设备</span>
        </button>
        <button class="deleteButton layui-btn layui-btn-radius layui-btn-primary"> 
            <i class="add-icon layui-icon layui-icon-delete" style="font-size: 15px;height: 20px;line-height: 20px;"></i>
            <span class="btn-inner" style="margin-top: 1px;">删除设备</span>
        </button>
    </div>
    <table class="layui-table" id="LAY-app-content-comm" lay-filter="LAY-app-content-comm"></table>

    <div class="footer layui-btn-containner">
        <button class="commit layui-btn layui-btn-radius layui-btn-primary" lay-submit
            lay-filter="LAY-submit-tj">提交</button>

        <button class="cancel layui-btn layui-btn-radius layui-btn-primary" lay-submit
            lay-filter="LAY-submit-qx">返回</button>
    </div>

</div>

<script type="text/html" id="buttonTpl">
    <span class="table-delete" lay-event="del"></span>
</script>

<!-- 巡检设备选择 -->
<script type="text/html" id="addDeviceTpl">
    <div class="leon-style-alert">
        <div class="layui-alert-box">
            <div class="layui-alert-title">
                <span class="layui-title-inner">添加设备选择</span>
                <span class="layui-title-screen">
                    <div class="screen-btn">
                        <form class="layui-form" action="">
                            <input type="search" name="keyWord" class="search-blank-inner searchInput"
                                placeholder="搜索" autocomplete="off"/>
                            <button type="button" lay-submit lay-filter="queryTableDeviceHeader"
                                class="search-blank-icon"></button>
                        </form>
                    </div>
                </span>
            </div>
            <div class="layui-content">
                <div class="con1 toggle-btn">
                    <span class="screen">
                        <i class="query-icon  layui-icon"></i>
                        <span class="query-inner">查询条件</span>
                    </span>
                    <i class="icon layui-icon layui-icon-down"></i>
                </div>
                <div class="con2 toggle-content">
                    <form action="" class="layui-form">


                        <label for="">设备编号:</label>
                        <input type="text" name="fia01" />
                        <label for="">设备名称:</label>
                        <input type="text" name="fia02" />
                        <label for="">供应商:</label>
                        <input type="text" name="fia09" />
                        <button type="button" lay-submit lay-filter="queryTableDevice"
                            class="query layui-btn layui-btn-radius layui-btn-primary">
                            <span class="search-icon"></span>
                            <span class="btn-inner">查询</span>
                        </button>
                    </form>
                </div>
                <table class="layui-table" id="table-device-alert"></table>
            </div>
            <div class="footer layui-btn-containner">
                <button class="btn1 layui-btn layui-btn-radius layui-btn-primary">添加</button>
                <button class="btn2 layui-btn layui-btn-radius layui-btn-primary">取消</button>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="buttonTpl2">
    <span>{{d.tcFtyName?d.tcFtyName:''}}{{d.tcFlrName?d.tcFlrName:''}}{{d.tcProcName?d.tcProcName:''}}{{d.tcSeriesName?d.tcSeriesName:''}}</span>
</script>

<script>
    layui.use(['admin', 'form', 'user', 'api', 'table', 'laydate'], function () {
        var $ = layui.$,
            setter = layui.setter,
            admin = layui.admin,
            form = layui.form,
            laydate = layui.laydate,
            router = layui.router(),
            api = layui.api,
            table = layui.table,
            search = router.search;
        var session = JSON.parse(sessionStorage.getItem('id'));
        var href = JSON.parse(sessionStorage.getItem('layui-this'));
        $(href).addClass('layui-this');
        // 保存
        form.on('submit(LAY-submit-bc)', function (data) {
            var field = data.field
            var data = {
                fil01: layui.router().search.fil01,
                fil07: $('#fil07').val(),
                fil21: $('#fil21').val(),
                fil12: $('#test1').val()
            }
            admin.req({
                url: api.host + api.updateFilBF_hhj,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                done: function (res) {
                    layer.open({
                        content:res.message
                    })
                    $('#fil07').val(res.data.fil07)
                    $('#fil21').val(res.data.fil21)
                    $('#test1').val(res.data.fil12)

                }
            });

        })
        // 取消
        form.on('submit(LAY-submit-qx)', function (data) {
            location.hash = '/transaction/cash'
        })
        // 提交
        form.on('submit(LAY-submit-tj)', function (data) {
            var field = data.field
            var data = {
                fil01: layui.router().search.fil01
            }
            admin.req({
                url: api.host + api.insertEquipmentTYOrBF1,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                done: function (res) {

                    location.hash = '/transaction/cash'
                }
            });
        })
        rander()
        function rander() {
            table.render({
                elem: '#LAY-app-content-comm',
                url: api.host + api.selectAssociatedDevice,
                where: {
                    fil01: layui.router().search.fil01
                }, //设定异步数据接口的额外参数
                skin: 'nob',
                headers: {
                    'token': layui.sessionData(setter.tableName).token
                },
                cols: [
                    [
                    {
                        type: 'checkbox'
                    },
                    {
                        field: 'fia01',
                        title: '设备编号',

                    }, {
                        field: 'fia02',
                        title: '设备名称',

                    },
                    {
                        field: 'fic02',
                        title: '设备类型',
                    },
                    {
                        field: 'pmc03',
                        title: '供应商',
                    }, {
                        field: 'tcSeriesName',
                        title: '设备系列',
                    }, {
                        field: 'fia05',
                        title: '设备型号',
                    }, {
                        field: 'fia011',
                        title: '资产编号',
                    }, {
                        field: 'azp02',
                        title: '所属公司',
                    }, {
                        field: 'gem02',
                        title: '负责部门',
                    }, {
                        field: 'tcFtyName',
                        title: '负责位置',
                        templet: '#buttonTpl2',
                    }, {
                        field: 'gen02',
                        title: '负责人',
                    }, {
                        title: '操作',
                        templet: '#buttonTpl',
                    }
                    ]
                ],
                request: {
                    pageName: 'pageNum' //页码的参数名称，默认：page
                    ,
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                page: true,
                limit: 10,
                text: {
                    none: '暂无数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                limits: [10, 15, 20, 25, 30],
                parseData: function (res) { //res 即为原始返回的数据
                    if (res.code == -1) {
                        layer.open({
                            content:res.message
                        })
                        return
                    }
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    }
                }
            });
           
            table.on('tool(LAY-app-content-comm)', function (obj) {
                var data = obj.data;
                var arrays = [];
                arrays.push(obj.data.fia01)
                if (obj.event === 'del') {
                    var data = {
                        fil01: layui.router().search.fil01,
                        fia01: arrays
                    }
                    layer.confirm('确定要删除该设备吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        admin.req({
                            url: api.host + api.dModificationPreservation,
                            data: JSON.stringify(data),
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            done: function (res) {
                                layer.open({content:res.message})
                                layui.table.reload('LAY-app-content-comm'); //重载表格
                                // layer.closeAll()
                            }
                        });
                    }, function () {
                        layer.close();
                    });
                }
            });
        }

        var filplant = ""
        admin.req({
            url: api.host + api.selectWorkOrderByTY,
            data: {
                fil01: layui.router().search.fil01
            },
            done: function (res) {
                var text = ''
                if (res.data[0].fil05 == 0) {
                    text = '未提交'
                } else if (res.data[0].fil05 == 1) {
                    text = '待审核'
                } else if (res.data[0].fil05 == 6) {
                    text = 'OA送审中'
                }

                //执行一个laydate实例
                laydate.render({
                    elem: '#test1' //指定元素
                    ,
                    value: res.data[0].fil12,
                    type: 'datetime',
                    format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
                });
                $('#fil05').html(text)
                $('#fil02').html(res.data[0].fil02)
                $('#fil07').val(res.data[0].fil07)
                $('#fil21').val(res.data[0].fil21)
                $('#fil01').html(res.data[0].fil01)
                $('#gen02').html(res.data[0].gen02)
                $('#oldazp').html(res.data[0].azp02)
                $('#fil21').html(res.data[0].fil21)
                $('#tcReqName').html(res.data[0].tcReqName)
                $('#newazp').html(res.data[0].gen02)
                $('#fil10').html(res.data[0].fil10)
                filplant = res.data[0].filplant
            }
        });

        $('.title-btn').on('click', '.add', function (e) {
            layer.open({
                type: 1,
                title: false,
                area: ['662px', '536px'],
                fixed: true,
                shadeClose: true,
                closeBtn: 0,
                content: $('#addDeviceTpl').html(),
                success: alertEvent.addDevice
            });
        })
        //批量删除
        $(".deleteButton").click(function(){
            var checkStatus = table.checkStatus('LAY-app-content-comm'); 
            var arrs = [];
            var fil01 =  layui.router().search.fil01;
            for (var i = 0; i < checkStatus.data.length; i++) {
                arrs.push(checkStatus.data[i].fia01);
            }
            if (checkStatus.data.length == 0) {
                parent.layer.msg('请至少选择一条数据', { icon: 2 });
                return;
            }
            admin.req({
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json',
                    url: api.host + api.dModificationPreservation,
                    data: JSON.stringify({fia01:arrs,fil01}),
                    success: function (data) {
                        layer.msg(data.message);
                        layui.table.reload('LAY-app-content-comm');
                    }
                });
             
        })
        var alertEvent = {
            addDevice: function (layero, index) {
                table.render({
                    elem: '#table-device-alert',
                    id: "#table-device-alert",
                    skin: 'nob',
                    height: 300,
                    loading: true,
                    method: 'POST',
                    toolbar: '',
                    headers: {
                        'token': layui.sessionData(setter.tableName).token
                    },
                    cols: [
                        [{
                            type: 'checkbox',
                            width: 20
                        },
                        {
                            field: 'fia01',
                            title: '设备编号'
                        },
                        {
                            field: 'fia02',
                            title: '设备名称'
                        },
                        {
                            field: 'fic02',
                            title: '设备类型'
                        },
                        {
                            field: 'pmc081',
                            title: '供应商'
                        },
                        {
                            field: 'tcSeriesName',
                            title: '设备系列'
                        },
                        {
                            field: 'fii02',
                            title: '设备型号'
                        }
                        ]
                    ],

                    url: api.host + api.FilFileBFSelectFiaFiles_hhj,
                    request: {
                        pageName: 'pageNum',
                        limitName: 'pageSize',
                    },
                    page: {
                        layout: ['prev', 'page', 'next', 'skip'],
                        limits: [10, 20, 30, 50],
                        theme: '#1E9FFF'
                    },
                    parseData: function (res) { //res 即为原始返回的数据
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.message, //解析提示文本
                            "count": res.data.total, //解析数据长度
                            "data": res.data //解析数据列表
                        };
                    },
                    contentType: 'application/json',
                    where: {
                        tcReqCoorig: filplant,
                        fiaacti: $('#fil05').val(),
                        fia01: "",
                        fia02: "",
                        fia09: "",
                        keyWord: "",
                    }
                });
                form.on('submit(queryTableProjectHeader)', function (data) {
                    var allData = data.field;
                    table.reload('#table-device-alert', {
                        where: allData,
                    });
                    return false;
                })
                form.on('submit(queryTableDevice)', function (data) {
                    var allData = data.field;
                    table.reload('#table-device-alert', {
                        where: allData,
                    });
                    return false;
                })

                $('.footer').on('click', '.btn2', function (e) {
                    layer.closeAll()
                })
                $('.footer').on('click', '.btn1', function (e) {
                        var checkedArr = table.checkStatus('#table-device-alert').data;
                        layui.router().search.fil01
                        var data = {
                            fil01: layui.router().search.fil01,
                            fia01: []
                        }
                        for (let index = 0; index < checkedArr.length; index++) {
                            data.fia01.push(checkedArr[index].fia01)
                        }
                        admin.req({
                            url: api.host + api.iModificationPreservation,
                            type: "post",
                            contentType: 'application/json',
                            dataType: 'json',
                            data: JSON.stringify(data),
                            done: function (res) {
                                layer.open({
                                    content:res.message
                                })
                                rander()
                                layer.closeAll()

                            }
                        });
                });
            }
        }




    });
</script>